package com.cskaoyan.service.wx.user.impl;

import com.cskaoyan.bean.MarketOrder;
import com.cskaoyan.bean.MarketOrderExample;
import com.cskaoyan.bean.vo.wx.user.UserIndexOfOrderVO;
import com.cskaoyan.bean.vo.wx.user.UserIndexVO;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.service.wx.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Mr.Tang
 * @since 2022/09/09 15:48
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    MarketOrderMapper marketOrderMapper;

    /**
     * 微信个人index显示
     * @return
     */
    @Override
    public UserIndexVO index(Integer id) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setUserId(id);
        // 生成两个响应对象，先向第二类响应封装数据
        UserIndexVO userIndexVO = new UserIndexVO();
        UserIndexOfOrderVO userIndexOfOrderVO = new UserIndexOfOrderVO();
        // 查找数据
//        MarketOrder order = marketOrderMapper.selectByPrimaryKey(id);


        // 查询待收货封装
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();
        criteria.andDeletedEqualTo(false);
//        criteria.andOrderStatusEqualTo((short) 301);
        ArrayList<Short> shorts = new ArrayList<Short>();
        shorts.add((short) 301);
        shorts.add((short) 402);
        criteria.andOrderStatusIn(shorts);
        criteria.andUserIdEqualTo(id);
        List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(marketOrderExample);
        // l和size都可以用，两个选一个就行
        long l = marketOrderMapper.countByExample(marketOrderExample);
        int size = marketOrders.size();
        userIndexOfOrderVO.setUnrecv((int) l);
        // 查询待发货封装
        MarketOrderExample marketOrderExample1 = new MarketOrderExample();
        MarketOrderExample.Criteria criteria1 = marketOrderExample1.createCriteria();
        criteria1.andDeletedEqualTo(false);
        criteria1.andOrderStatusEqualTo((short) 201);
        criteria1.andUserIdEqualTo(id);
        List<MarketOrder> marketOrders1 = marketOrderMapper.selectByExample(marketOrderExample1);
        int size1 = marketOrders1.size();
        userIndexOfOrderVO.setUnship(size1);
        // 查询待付款
        MarketOrderExample marketOrderExample2 = new MarketOrderExample();
        MarketOrderExample.Criteria criteria2 = marketOrderExample2.createCriteria();
        criteria2.andDeletedEqualTo(false);
        criteria2.andUserIdEqualTo(id);
        criteria2.andOrderStatusEqualTo((short) 101);
        List<MarketOrder> marketOrders2 = marketOrderMapper.selectByExample(marketOrderExample2);
        int size2 = marketOrders2.size();
        userIndexOfOrderVO.setUnpaid(size2);


        // 查询待评价封装
        MarketOrderExample marketOrderExample3 = new MarketOrderExample();
        MarketOrderExample.Criteria criteria3 = marketOrderExample3.createCriteria();
//        criteria3.andCommentsEqualTo(order.getComments());
        criteria3.andDeletedEqualTo(false);
//          criteria3.andCommentsEqualTo((short) 0);
        // 当comment大于0且status等于401（已收货状态时才显示待评论）
          criteria3.andCommentsGreaterThan((short) 0);
          criteria3.andOrderStatusEqualTo((short) 401);
          criteria3.andUserIdEqualTo(id);
        List<MarketOrder> marketOrders3 = marketOrderMapper.selectByExample(marketOrderExample3);
        int size3 = marketOrders3.size();
        userIndexOfOrderVO.setUncomment(size3);
        userIndexVO.setOrder(userIndexOfOrderVO);
        return userIndexVO;
    }
}
